﻿using System;
using System.Web.UI.WebControls;
using Model;
using Model.Entities;
using MusicSite.ViewClasses;

namespace MusicSite.Views.Content.Album
{
    public partial class IndexAlb : BasePage
    {
        private UserProvider usProv = new UserProvider();
        protected void Page_Load(object sender, EventArgs e)
        {
            CheckAuthentication();
            UserRole roleId = (UserRole)(Session["UserRole"]);
            switch (roleId)
            {
                case UserRole.Administrator:
                    break;
                case UserRole.Registrator:
                    HyperLink admLink1 = (HyperLink)Master.FindControl("adminLink");
                    admLink1.Visible = false;
                    break;
                case UserRole.User:
                    HyperLink admLink = (HyperLink)Master.FindControl("adminLink");
                    admLink.Visible = false;
                    GridView1.Columns[0].Visible = false;
                    hyperLink1.Visible = false;
                    break;
                default:
                    break;
            }
            ErrorName.Visible = false;
            ErrorYear.Visible = false;
            ErrorStyle.Visible = false;
            ErrorArtName.Visible = false;
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var image = (FileUpload) GridView1.Rows[e.RowIndex].FindControl("CoverUpload");
            e.NewValues["Cover"] = image.FileName;
            SaveImageOnDisc(image);
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
        }

        public void SaveImageOnDisc(FileUpload img)
        {
            if (string.IsNullOrEmpty(img.FileName) || img.FileBytes.Length == 0)
                return;
            string dir = Server.MapPath("~/Content/images/" + img.FileName);
            img.SaveAs(dir);
        }
    }
}
